package transactions.dq.policy;

import dinamica.Db;
import dinamica.GenericTableManager;
import dinamica.Recordset;
import dinamica.StringUtil;

public class PolicyHouse extends GenericTableManager
{
	
	public int service(Recordset inputParams) throws Throwable
	{
		int rc = super.service(inputParams);
		//获取sql文件
		String delete = getSQL(getResource("delete.sql"),inputParams);
		String insert = getResource("insert_house.sql");
		//获取页面数据集
		String house_type[] = getRequest().getParameterValues("house_type");
		String house_level[] = getRequest().getParameterValues("house_level");
		String zhihuan_price[] = getRequest().getParameterValues("zhihuan_price");
		String remark[] = getRequest().getParameterValues("remark");
		
		Db db = getDb();
		//先删除原数据，再重新插入新数据
		db.addBatchCommand(delete);
		
		//必填项不能为空
		if(house_type==null || house_level==null || zhihuan_price==null  || remark==null){
			throw new Throwable("数据填写不完整，请确认后再提交。");
		}
		int length = house_level.length;
		
		//添加批处理插入语句
		for(int i=0;i<length;i++)
		{
			if(house_type[i] == null || house_type[i].trim().length() == 0){
				throw new Throwable("房屋类别不能为空，请确认后再提交。");
			}
			if(house_level[i] == null || house_level[i].trim().length() == 0){
				throw new Throwable("房屋等级不能为空，请确认后再提交。");
			}
			if(zhihuan_price[i] == null || zhihuan_price[i].trim().length() == 0){
				throw new Throwable("产权置换价格不能为空，请确认后再提交。");
			}
			try{
				Double.valueOf(zhihuan_price[i]);
			}catch(NumberFormatException e){
				throw new Throwable("产权置换价格格式不正确，请确认后再提交。");
			}
			
			if(remark[i] != null && remark[i].trim().length() > 120){
				throw new Throwable("备注输入太长，不能超过120个字符，请确认后再提交。");
			}
			
			String insert_sql = "";
			insert_sql = StringUtil.replace(insert, "${house_type}", house_type[i]);
			insert_sql = StringUtil.replace(insert_sql, "${house_level}", house_level[i]);
			insert_sql = StringUtil.replace(insert_sql, "${zhihuan_price}", zhihuan_price[i]);
			insert_sql = StringUtil.replace(insert_sql, "${remark}", remark[i]);
			
			insert_sql = getSQL(insert_sql,inputParams);
			db.addBatchCommand(insert_sql);
		}
		
		db.exec();
		return rc;
	}

}
